package com.xbongbong.pro.statistic.service.result;

import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.ElasticConstant;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPkPojo;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultDetailVO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultPkDetailVO;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.stereotype.Component;

import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * 联系人
 * @author zcp
 * @version v1.0
 * @date 2019/3/19 20:57
 * @since v1.0
 */
@Component
public class CrmContactResult extends ChartResultParentService implements ChartResultSystem, ChartResultPk {

    @Override
    public Object getTarget(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        return null;
    }

    @Override
    public Object getValueTarget(ChartResultPojo chartResultPojo) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case CONTACT_ADD_BOARD:
                return contactAddBoard(chartResultPojo);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getPkTarget(UserEntity userEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPkPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //pk、完成情况（员工排行）
            case PK_CONTACT_ADD:
            case PERFORMANCE_CONTACT_ADD:
                return pkContactAdd(userEntity, chartResultPkPojo, chartResultPkDetailVO);
            default:
                break;
        }
        return null;
    }

    /**
     * 系统pk范围规则指标公共接口
     *
     * @param userEntity            登录员工
     * @param chartEntity
     * @param chartResultPkPojo     pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/18 15:34
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Object getPkRuleTarget(UserEntity userEntity, ChartEntity chartEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPkPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //pk、完成情况（员工排行）
            case PK_CONTACT_ADD:
            case PERFORMANCE_CONTACT_ADD:
                return pkRuleContactAdd(userEntity, chartEntity, chartResultPkPojo, chartResultPkDetailVO);
            default:
                break;
        }
        return null;
    }

    /**
     * 新增联系人数
     * @param chartResultPojo
     * @return java.lang.String 
     * @author zcp
     * @date 2019/3/29 14:17
     * @since v1.0
     * @version v1.0
     */
    public String contactAddBoard(ChartResultPojo chartResultPojo) throws XbbException {
        /*--------处理查询条件--------*/
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.boolQueryBuilder4Create(chartResultPojo, "addTime");
        ElasticAggsPojo elasticAggsPojo = esHelper.elasticAggs(IndexTypeEnum.IDX_SAAS_CONTACT, boolQueryBuilder, null);
        return elasticAggsPojo.getTotal();
    }

    /**
     * 新增联系人pk
     * @param userEntity 登录员工信息
     * @param chartResultPkPojo pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return com.xbongbong.pro.statistic.pojo.vo.ChartResultPkDetailVO
     * @throws XbbException
     * @author zcp
     * @date 2019/3/19 21:22
     * @since v1.0
     * @version v1.0
     */
    public ChartResultPkDetailVO pkContactAdd(UserEntity userEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        String fieldTime = "addTime";
        /*--------处理查询条件--------*/
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.boolQueryBuilder4LimitStart(chartResultPkPojo, fieldTime);
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("aggs01").field("creatorId.keyword");
        termsAggregationBuilder.executionHint(ElasticConstant.EXECUTION_HINT);
        int pkType = chartResultPkPojo.getPkType();
        return pkHelp.getPkResult4Count(userEntity, chartResultPkPojo, chartResultPkDetailVO, IndexTypeEnum.IDX_SAAS_CONTACT, boolQueryBuilder, termsAggregationBuilder, pkType);
    }

    /**
     * 新增联系人pk
     * @param userEntity 登录员工信息
     * @param chartResultPkPojo pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return com.xbongbong.pro.statistic.pojo.vo.ChartResultPkDetailVO
     * @throws XbbException
     * @author zcp
     * @date 2019/3/19 21:22
     * @since v1.0
     * @version v1.0
     */
    public ChartResultPkDetailVO pkRuleContactAdd(UserEntity userEntity, ChartEntity chartEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        String fieldTime = "addTime";
        /*--------处理查询条件--------*/
        BoolQueryBuilder boolQueryBuilder = chartRuleQueryHelp.boolQueryBuilder4LimitStart(chartResultPkPojo, fieldTime);
        //限制创建人id、且非主管或筛选
        if (chartResultPkPojo.getPerformancePk()  && chartDtoHelp.judgePermissionAndIfCheck(chartResultPkPojo)) {
            boolQueryBuilder.filter(termsQuery("creatorId.keyword", chartResultPkPojo.getUserIdIn()));
        }
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("aggs01").field("creatorId.keyword");
        termsAggregationBuilder.executionHint(ElasticConstant.EXECUTION_HINT);
        int pkType = chartResultPkPojo.getPkType();
        return pkRuleHelp.getPkResult4Count(userEntity, chartEntity, chartResultPkPojo, chartResultPkDetailVO, IndexTypeEnum.IDX_SAAS_CONTACT, boolQueryBuilder, termsAggregationBuilder, pkType);
    }

}
